package com.leetcode.linkedList;

import com.leetcode.basic.ListNode;

/**
 * @author Dennis Li
 * @date 2020/6/11 22:53
 */
public class AddTwoNumbers_445 {
    // 只需要O(1)的空间
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int c = 0;
        ListNode dummy = new ListNode(-1), cur = dummy;
        while (l1 != null || l2 != null || c != 0) {
            int v1 = 0, v2 = 0;
            if (l1 != null) v1 = l1.val;
            if (l2 != null) v2 = l2.val;
            int sum = (v1 + v2 + c) % 10;
            cur.next = new ListNode(sum);
            c = (v1 + v2 + c) / 10;
            if (l1 != null) l1 = l1.next;
            if (l2 != null) l2 = l2.next;
            cur = cur.next;
        }
        return dummy.next;
    }

}
